/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UfSelector.java
 *
 * Created on Apr 8, 2009, 4:37:53 PM
 */

package doghost.selectors;
import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.UfController;
import doghost.entities.Uf;


/**
 *
 * @author duo
 */
public class UfSelector extends javax.swing.JDialog {

    protected UfController ufController;
    protected Uf selectedUf;

    /** Creates new form UfSelector */
    public UfSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        ufController = new UfController();
        ufTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("ufRecordSelected",
                                !isUfRecordSelected(), isUfRecordSelected());
                    }
                });
    }

    public Uf getSelectedUf() {
        return selectedUf;
    }

    public void setSelectedUf(Uf selectedUf) {
        this.selectedUf = selectedUf;
    }

    public boolean isUfRecordSelected() {
        return ufTable.getSelectedRow() != -1;
    }

    @Action
    public void newUf() {
        ufController.newUf(doghostPUEntityManager, ufList);
    }

    @Action(enabledProperty = "ufRecordSelected")
    public void editUf() {
        int index = ufTable.getSelectedRow();
        ufController.setUf(ufList.get(ufTable.
                convertRowIndexToModel(index)));
        ufController.editUf(doghostPUEntityManager, ufList,
                ufTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "ufRecordSelected")
    public void deleteUf() {
        int index = ufTable.getSelectedRow();
        ufController.setUf(ufList.get(ufTable.
            convertRowIndexToModel(index)));
        ufController.deleteUf(doghostPUEntityManager, ufList,
            ufTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelUfSelector() {
        setSelectedUf(null);
        setVisible(false);
    }

    @Action(enabledProperty = "ufRecordSelected")
    public void selectUf() {
        int index = ufTable.getSelectedRow();
        setSelectedUf(ufList.get(ufTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterUf() {
        TableRowSorter ufSorter = (TableRowSorter) ufTable.getRowSorter();
        String text = ufFilterTextField.getText();
        ufFilterTextField.setText("");
        if(text.length()==0) {
            ufSorter.setRowFilter(null);
        } else {
            ufSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(UfSelector.class);
        ufQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery(resourceMap.getString("ufQuery.query")); // NOI18N
        ufList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(ufQuery.getResultList());
        ufFilterTextField = new javax.swing.JTextField();
        ufFilterButton = new javax.swing.JButton();
        ufSelectionPanel = new org.jdesktop.swingx.JXPanel();
        ufScrollPane = new javax.swing.JScrollPane();
        ufTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        ufFilterTextField.setText(resourceMap.getString("ufFilterTextField.text")); // NOI18N
        ufFilterTextField.setName("ufFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(UfSelector.class, this);
        ufFilterButton.setAction(actionMap.get("filterUf")); // NOI18N
        ufFilterButton.setText(resourceMap.getString("ufFilterButton.text")); // NOI18N
        ufFilterButton.setName("ufFilterButton"); // NOI18N

        ufSelectionPanel.setName("ufSelectionPanel"); // NOI18N

        ufScrollPane.setName("ufScrollPane"); // NOI18N

        ufTable.setAutoCreateRowSorter(true);
        ufTable.setName("ufTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, ufList, ufTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sigla}"));
        columnBinding.setColumnName("Sigla");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        ufScrollPane.setViewportView(ufTable);
        ufTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("ufTable.columnModel.title0")); // NOI18N
        ufTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("ufTable.columnModel.title1")); // NOI18N

        javax.swing.GroupLayout ufSelectionPanelLayout = new javax.swing.GroupLayout(ufSelectionPanel);
        ufSelectionPanel.setLayout(ufSelectionPanelLayout);
        ufSelectionPanelLayout.setHorizontalGroup(
            ufSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 399, Short.MAX_VALUE)
            .addGroup(ufSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ufSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(ufScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        ufSelectionPanelLayout.setVerticalGroup(
            ufSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
            .addGroup(ufSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ufSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(ufScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        newButton.setAction(actionMap.get("newUf")); // NOI18N
        newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editUf")); // NOI18N
        editButton.setText(resourceMap.getString("editButton.text")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteUf")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelUfSelector")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ufFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ufFilterButton)
                .addContainerGap(178, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addGap(18, 18, 18)
                        .addComponent(editButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton))
                    .addComponent(ufSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ufFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ufFilterButton))
                .addGap(18, 18, 18)
                .addComponent(ufSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                UfSelector dialog = new UfSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton ufFilterButton;
    private javax.swing.JTextField ufFilterTextField;
    private java.util.List<doghost.entities.Uf> ufList;
    private javax.persistence.Query ufQuery;
    private javax.swing.JScrollPane ufScrollPane;
    private org.jdesktop.swingx.JXPanel ufSelectionPanel;
    private javax.swing.JTable ufTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
